package com.sright.bcard.enterprise.biz.manager.impl;

import com.sright.bcard.common.biz.manager.ContentCheckManager;
import com.sright.bcard.common.service.share.dto.page.PageDTO;
import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.enums.SourceTypeEnum;
import com.sright.bcard.common.service.share.exception.base.BusinessErrorException;
import com.sright.bcard.common.service.share.exception.code.BusinessCode;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.enterprise.biz.manager.CompanyManager;
import com.sright.bcard.enterprise.dal.dao.CompanyDAO;
import com.sright.bcard.enterprise.dal.entity.CompanyDO;
import com.sright.bcard.common.service.share.dto.enterprise.CompanyDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class CompanyManagerImpl implements CompanyManager {

    @Autowired
    private CompanyDAO companyDAO;

    @Autowired
    private ContentCheckManager contentCheckManager;

    @Override
    public PageDTO<CompanyDTO> companyList(PageParamRequest pageParamRequest) {
        // 查询总的条数
        Integer count = companyDAO.count(pageParamRequest);
        PageDTO<CompanyDTO> pageDTO = new PageDTO<>();
        // 分页查询数据
        List<CompanyDO> companyDTOList = companyDAO.pageList(pageParamRequest);
        if(CollectionUtils.isNotEmpty(companyDTOList)) {
            // DTO 转化为 DO
            List<CompanyDTO> companyDTOS = new ArrayList<>();
            DataConvertUtils.convertDOList2DTOList(companyDTOList, companyDTOS, CompanyDTO.class);
            pageDTO.setData(companyDTOS);
            pageDTO.setCount(count);
            return pageDTO;
        }
        return new PageDTO<>();
    }

    @Override
    public Boolean addCompany(CompanyDTO companyDTO) {
        // DTO转化为DO
        if(null == companyDTO) {
            return false;
        }
        //新增敏感词审核
        if (StringUtils.isNotEmpty(companyDTO.getName())) {
            List<String> contentList = new LinkedList<>();
            contentList.add(companyDTO.getName());
            String checkResult = contentCheckManager.textCheck(contentList,companyDTO.getEnterpriseId(), SourceTypeEnum.ENTERPRISE_BACKEND.getValue());
            if (checkResult.equals("block")) {
                throw new BusinessErrorException(BusinessCode.CONTAIN_SENSITIVE_WORDS);
            }
        }
        CompanyDO companyDO = new CompanyDO();
        DataConvertUtils.convertDTO2DO(companyDTO, companyDO);
        return companyDAO.insertSelective(companyDO) == 1;
    }

    @Override
    public Boolean updateCompany(CompanyDTO companyDTO) {
        if(null == companyDTO) {
            return false;
        }
        //新增敏感词审核
        if (StringUtils.isNotEmpty(companyDTO.getName())) {
            List<String> contentList = new LinkedList<>();
            contentList.add(companyDTO.getName());
            String checkResult = contentCheckManager.textCheck(contentList,companyDTO.getEnterpriseId(), SourceTypeEnum.ENTERPRISE_BACKEND.getValue());
            if (checkResult.equals("block")) {
                throw new BusinessErrorException(BusinessCode.CONTAIN_SENSITIVE_WORDS);
            }
        }
        CompanyDO companyDO = new CompanyDO();
        DataConvertUtils.convertDTO2DO(companyDTO, companyDO);
        return companyDAO.updateSelective(companyDO);
    }

    @Override
    public Boolean deleteCompany(List<Long> ids) {
        final Integer[] count = {0};
        if(CollectionUtils.isNotEmpty(ids)) {
            ids.stream().forEach(id -> {
                Integer i = companyDAO.deleteCompany(id);
                if(i == 1) {
                    count[0]++;
                }
            });
        }
        return count[0] > 0;
    }

    @Override
    public List<CompanyDTO> getList(Long enterpriseId) {
        List<CompanyDO> companyDOS = companyDAO.selectByEnterpriseId(enterpriseId);
        // do 转 DTO
        if(CollectionUtils.isNotEmpty(companyDOS)) {
            List<CompanyDTO> companyDTOS = new ArrayList<>();
            DataConvertUtils.convertDOList2DTOList(companyDOS, companyDTOS, CompanyDTO.class);
            return companyDTOS;
        }
        return new ArrayList<>();
    }
}
